package task

import (
	"context"
	"dataaysn/internal/model"

	"github.com/robfig/cron/v3"
)

type Scheduler struct {
	cron  *cron.Cron
	tasks map[uint]*model.Task
}

func NewScheduler() *Scheduler {
	return &Scheduler{
		cron:  cron.New(),
		tasks: make(map[uint]*model.Task),
	}
}

func (s *Scheduler) AddTask(task *model.Task) error {
	_, err := s.cron.AddFunc(task.CronExpr, func() {
		// 任务执行逻辑
	})

	if err == nil {
		s.tasks[task.ID] = task
	}
	return err
}

func (s *Scheduler) Start(ctx context.Context) {
	s.cron.Start()
	<-ctx.Done()
	s.cron.Stop()
}
